package com.example.testing.ipcdemo.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by tangjiangxu on 9/12/17.
 */

public class BookProvider extends ContentProvider {
    private static final String TAG = "BookProvider";
    private static final String AUTHORITY = "com.josan.book.provider";
    public static final Uri BOOK_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/book");
    public static final Uri USER_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/user");

    private static final int BOOK_URI_CODE = 1;
    private static final int USER_URI_CODE = 2;

    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        sUriMatcher.addURI(AUTHORITY, "book", BOOK_URI_CODE);
        sUriMatcher.addURI(AUTHORITY, "user", USER_URI_CODE);
    }


    private Context mContext;
    private SQLiteDatabase mDb;

    @Override
    public boolean onCreate() {
        Log.e(TAG, "onCreate: current thread:" + Thread.currentThread().getName());
        mContext = getContext();
        //ContentProvider创建时，初始化数据库。注意：这里只是演示，实际使用中不推荐在主线程中进行耗时的数据库操作
        initProviderData();
        return false;
    }

    private void initProviderData() {
        mDb = new DbOpenHelper(mContext).getWritableDatabase();
        mDb.execSQL("delete from " + DbOpenHelper.BOOK_TABLE_NAME);
        mDb.execSQL("delete from " + DbOpenHelper.USER_TABLE_NAME);
        mDb.execSQL("insert into book values(3, 'Android')");
        mDb.execSQL("insert into book values(4, 'IOS')");
        mDb.execSQL("insert into user values(1, 'josan', 1)");
        mDb.execSQL("insert into user values(2, 'tang', 24)");
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Log.e(TAG, "query: current thread:" + Thread.currentThread().getName());
        String table = getTableName(uri);
        if (table != null) {
            return mDb.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        Log.e(TAG, "getType: current thread:" + Thread.currentThread().getName());
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Log.d(TAG, "insert: current thread:" + Thread.currentThread().getName());
        String tableName = getTableName(uri);
        if (tableName != null) {
            mDb.insert(tableName, null, values);
            mContext.getContentResolver().notifyChange(uri, null);
            return uri;
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        Log.e(TAG, "delete: current thread:" + Thread.currentThread().getName());
        String table = getTableName(uri);
        if (table != null) {
            int count = mDb.delete(table, selection, selectionArgs);
            if (count > 0 ) {
                mContext.getContentResolver().notifyChange(uri, null);
            }
        }
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        Log.e(TAG, "update: current thread:" + Thread.currentThread().getName());
        String table = getTableName(uri);
        if (table != null) {
            int row = mDb.update(table, values, selection, selectionArgs);
            if (row > 0) {
                mContext.getContentResolver().notifyChange(uri, null);
                return row;
            }
        }
        return 0;
    }

    private String getTableName(Uri uri) {
        String tableName = null;
        switch (sUriMatcher.match(uri)) {
            case BOOK_URI_CODE:
                tableName = DbOpenHelper.BOOK_TABLE_NAME;
                break;
            case USER_URI_CODE:
                tableName = DbOpenHelper.USER_TABLE_NAME;
                break;
        }
        return tableName;
    }
}
